package com.zhaoxi.project.wms.controller;

import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.project.wms.service.impl.BomServiceImpl;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.page.TableDataInfo;

/**
 * 生产物品Controller
 *
 * @author zhaoxi
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/wms/bom")
public class BomController extends BaseService
{
    @Autowired
    private BomServiceImpl BomService;

    /**
     * 查询生产物品分页列表
     * @param map
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:list')")
    @GetMapping("/getPage")
    public TableDataInfo getPage(Map map)
    {
        PageData pd = this.getPageData(map);
        startPage();
        List<PageData> list = BomService.getList(pd);
        return getDataTable(list);
    }

    /**
     * 查询生产物品列表
     * @param map
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:list')")
    @GetMapping("/getList")
    public PageData getList(Map map)
    {
        PageData pd = this.getPageData(map);
        List<PageData> list = BomService.getList(pd);
        return this.success(list);
    }

    /**
     * 获取生产物品详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:query')")
    @GetMapping(value = "/{productId}")
    public PageData getInfo(@PathVariable("productId") Long productId)
    {
        return success(BomService.getByProductId(productId));
    }

    /**
     * 查询生产物品列表行数
     * @param map
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:list')")
    @GetMapping("/getCount")
    public PageData getCount(Map map)
    {
        PageData pd = this.getPageData(map);
        return this.success(BomService.getCount(pd));
    }

    /**
     * 新增生产物品
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:add')")
    @Log(title = "生产物品", businessType = BusinessType.INSERT)
    @PostMapping
    public PageData add(@RequestBody Map map)
    {
        PageData pd = this.getPageData(map);
        BomService.add(pd);
        Long productId = pd.getLong("productId");
        if (productId > 0){
            pd.clear();
            pd.put("productId",productId);
            return this.success(pd);
        }
        return this.error();
    }

    /**
     * 修改生产物品
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:edit')")
    @Log(title = "生产物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public PageData edit(@RequestBody Map map)
    {
        PageData pd = this.getPageData(map);
        return toAjax(BomService.edit(pd));
    }

    /**
     * 删除生产物品
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:remove')")
    @Log(title = "生产物品", businessType = BusinessType.DELETE)
    @DeleteMapping
    public PageData delByProductIds(@RequestBody Long[] productIds)
    {
        return toAjax(BomService.delByProductIds(productIds));
    }


/**       ----子数据----      */
    /**
     * 查询物料清单明细列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:query')")
    @GetMapping("/getLineList")
    public PageData bomLineList(Map map)
    {
        PageData pd = this.getPageData(map);
        return success(BomService.getLineList(pd));
    }

    /**
     * 新增物料清单明细
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:add')")
    @Log(title = "新增物料清单明细", businessType = BusinessType.INSERT)
    @PostMapping("/line")
    public PageData addBomLine(@RequestBody Map map)
    {
        PageData pd = this.getPageData(map);
        return toAjax(BomService.addLine(pd));
    }

    /**
     * 修改物料清单明细
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:edit')")
    @Log(title = "修改物料清单明细", businessType = BusinessType.UPDATE)
    @PutMapping("/line")
    public PageData editBomLine(@RequestBody Map map)
    {
        PageData pd = this.getPageData(map);
        return toAjax(BomService.editLine(pd));
    }

    /**
     * 删除物料清单明细
     */
    @PreAuthorize("@ss.hasPermi('wms:bom:remove')")
    @Log(title = "删除物料清单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/line")
    public PageData removeBomLine(@RequestBody Long[] subProductIds)
    {
        return toAjax(BomService.delLineBySubProductIds(subProductIds));
    }
}
